package com.yj_app.qualitytest.activity.login;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.cardview.widget.CardView;

import com.lzy.okgo.model.Response;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.yj_app.qualitytest.R;
import com.yj_app.qualitytest.activity.home.HomeActivity;
import com.yj_app.qualitytest.activity.login.contract.LoginContract;
import com.yj_app.qualitytest.activity.login.presenter.LoginPresenter;
import com.yj_app.qualitytest.adapter.CompanyAdapter;
import com.yj_app.qualitytest.base.BaseMvpActivity;
import com.yj_app.qualitytest.callback.DialogCallback;
import com.yj_app.qualitytest.common.Config;
import com.yj_app.qualitytest.info.BaseInfo;
import com.yj_app.qualitytest.info.CompanyInfo;
import com.yj_app.qualitytest.info.Loginfo;
import com.yj_app.qualitytest.util.PreferencesManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gr.escsoft.michaelprimez.searchablespinner.SearchableSpinner;
import gr.escsoft.michaelprimez.searchablespinner.interfaces.OnItemSelectedListener;

import static com.yj_app.qualitytest.base.QtApplication.getContext;

/**
 * Copyright (C), 2015-2019/9/11, 石家庄云捷软件科技有限公司
 *
 * @ProjectName: LoginActivity.java
 * @Package: com.yj_app.qualitytest.activity.login
 * @ClassName: LoginActivity
 * @Description: TODO
 * @Author: Hu_Sir
 * @CreateDate: 2019/9/11--13:11
 * @UpdateUser: ?
 * @UpdateDate: 2019/9/11--13:11
 * @UpdateRemark: todo
 */
public class LoginActivity extends BaseMvpActivity<LoginContract.Presenter, LoginContract.View> implements LoginContract.View {

    @BindView(R.id.SearchableSpinner)
    SearchableSpinner mSearchableSpinner;
    @BindView(R.id.user_name)
    MaterialEditText mUserName;
    @BindView(R.id.user_pwd)
    MaterialEditText mUserPwd;
    @BindView(R.id.btn_login)
    Button mBtnLogin;
    @BindView(R.id.card_layout)
    CardView mCardLayout;
    private List<String> companys;
    CompanyAdapter companyAdapter;
    List<CompanyInfo> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
        initCompanysAdapter();
        initEvent();
        getCompany();




    }

    private void getCompany() {
        presenter.getCompanies();
    }

    @Override
    public LoginContract.Presenter initPresenter() {
        return new LoginPresenter();
    }

    private void initEvent() {

        mSearchableSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(View view, int position, long id) {
                if (position == 0) {
                    Toast.makeText(LoginActivity.this, "没有选择", Toast.LENGTH_SHORT).show();
                    PreferencesManager.getInstance(getContext()).remove(Config.COMPANY_INFO);
                } else {
                    PreferencesManager.getInstance(getContext()).put(Config.COMPANY_INFO, list.get(position - 1));
                    Toast.makeText(LoginActivity.this, companys.get(position - 1) + "", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onNothingSelected() {

            }
        });
    }

    private void initCompanysAdapter() {
        companyAdapter = new CompanyAdapter(this, companys);
        mSearchableSpinner = (SearchableSpinner) findViewById(R.id.SearchableSpinner);
        mSearchableSpinner.setAdapter(companyAdapter);
    }

    private void initView() {
        fullscreen(true);
        companys = new ArrayList<>();
    }

    @OnClick(R.id.btn_login)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_login:
                goLogin();


//                startActivity(new Intent(this, MainActivity.class));
                break;
        }
    }

    private void goLogin() {
        if (mSearchableSpinner.getSelectedPosition() == 0) {
            toastError("请选择公司后提交");
            return;
        }
        if (!checkNull(mUserName, "用户名不能为空")) {
            mUserName.setError("请输入用户名");
            return;
        }
        if (!checkNull(mUserPwd, "密码不能为空")) {
            mUserName.setError("请输入用密码");
            return;
        }
        if (getEdittext(mUserName).equals("sccj") && getEdittext(mUserPwd).equals("123456")) {

            Loginfo loginfo=new Loginfo();
            loginfo.setNickname("生产车间");
            loginfo.setGuid("");
            loginfo.setCode("sccj");
            PreferencesManager.getInstance(this).put(Config.LOGINFO, loginfo);
            startActivity(HomeActivity.class);
            finish();

        } else {
            presenter.login(getEdittext(mUserName), getEdittext(mUserPwd));
        }


    }

    @Override
    public void reLoginData(Loginfo body) {
        if (body.isSuccess()) {
            toastSuccess(body.getMsg());
            body.setCode(getEdittext(mUserName));
            PreferencesManager.getInstance(this).put(Config.LOGINFO, body);
            startActivity(HomeActivity.class);
            finish();
        } else {
            toastError(body.getMsg());
        }
    }


    @Override
    public void reCompaniesData(List<CompanyInfo> list) {
        if (list != null && list.size() > 0) {
            this.list = list;
            if (companys.size() != 0) {
                companys.clear();
            }
            CompanyInfo companyInfo = PreferencesManager.getInstance(this).get(Config.COMPANY_INFO, CompanyInfo.class);
            int selectpos = 0;
            for (int i = 0; i < list.size(); i++) {
                if (companyInfo != null) {
                    if (TextUtils.equals(companyInfo.getCorp(), list.get(i).getCorp())) {
                        selectpos = i + 1;
                    }
                }
                companys.add(list.get(i).getUnitname());
            }
            mSearchableSpinner.setSelectedItem(selectpos);
            companyAdapter.notifyDataSetChanged();
        } else {
            toastError("获取公司信息失败");
        }

    }
}
